package com.fishery.base.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fishery.base.entity.GermchitType;
import com.fishery.base.listener.GermchitTypeDataListener;
import com.fishery.base.mapper.GermchitTypeMapper;
import com.fishery.base.service.GermchitTypeService;
import com.fishery.base.util.ExcelUtil;
import com.fishery.base.util.InputStreamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@Service
public class GermchitTypeServiceImpl extends ServiceImpl<GermchitTypeMapper, GermchitType> implements GermchitTypeService {

	@Autowired
	private GermchitTypeDataListener germchitTypeDataListener;

	@Autowired
	private GermchitTypeMapper germchitTypeMapper;

	@Override
	public boolean saveDataToDBWithExcel(MultipartFile file) {
		InputStream is = null;
		try {
			is = file.getInputStream();
			EasyExcel.read(is, GermchitType.class, germchitTypeDataListener).sheet().doRead();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			InputStreamUtil.close(is);
		}
		return true;
	}

	@Override
	public void downloadData(HttpServletResponse response) {
		ExcelUtil.write(response, "种苗养殖环境说明",GermchitType.class, "种苗养殖环境", germchitTypeMapper.selectList(null));
	}
}
